// packageStudent/pages/collect-list/index.js
import Config from "../../../main.config"
import {getStudentCollectList} from "../../../service/Collection";
import {getStorageSync} from "../../../utils/Storage"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //投递列表
        collect: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data._registerId = getStorageSync(Config.storage.registerInfo + ".id");
        this.setData({"collect.loading": true});
    },

    /*
    * 列表加载
    * */
    onLoadList: async function (e) {
        try {
            let paging = e.detail.paging;
            let list = this.data.collect.list;
            let isReset = this.data.collect.reset;
            let registerId = this.data._registerId;
            let res = await getStudentCollectList({
                page: paging.page,
                limit: paging.pageSize,
                userId: registerId
            });
            let newList = res.data.map(function (item) {
                let logUrl = Config.api.downLoad + "?fileId=" + (item.staffLogo || item.companyLogo) || ""
                return {
                    id: item.id,
                    objectId: item.objectId,
                    name: item.name,
                    nature: item.nature,
                    salary: item.salary,
                    companyName: item.companyName,
                    scale: item.scale,
                    logo: logUrl,

                }
            });
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "collect.list": list,
                "collect.total": res.count
            });
        } catch (e) {
            console.error(e)
            this.setData({
                "collect.error": true
            })
        } finally {
            this.setData({
                "collect.reset": false,
                "collect.loading": false
            })
        }
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"collect.loading": true});
    }
})